package net.suncaper.demo.controller;
import net.suncaper.demo.domain.User;
import net.suncaper.demo.service.AdminService;
import net.suncaper.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import net.suncaper.demo.mapper.UserMapper;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    @GetMapping("/useradd")
    public String addPage(Model model,User user){
        model.addAttribute("user",new User());
        return "pages/user/useradd.html";
    }

    @PostMapping("/useradd")
    public String saveUser(User user){
        userService.saveUser(user);
        return "redirect:/user/list";
    }

    @PostMapping("/update")
    public String updateUser(User user){
        userService.updateUser(user);
        return "redirect:/user/list";
    }

    @GetMapping("/update/{userid}")
    public String editPage(@PathVariable("userid") Long userid, Model model){
        model.addAttribute("user",userService.findUserById(userid));
        return "/pages/user/update.html";
    }

    @GetMapping("/delete/{userid}")
    public String deleteUserById(@PathVariable("userid") Long userid){
        userService.deleteUserById(userid);
        return "redirect:/user/list";
    }


    @GetMapping("/list")
    public String userPage(Model model){
        List<User> users = userService.findUser();
        model.addAttribute("users",users);

        return "pages/user/list.html";
    }

/*    @GetMapping("/detail")
    public String showUser(User user){
        userService.findUser();
        return "redirect:/user/detail";
    }*/

    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    @GetMapping("/detail/{userid}")
    public String detailPage(@PathVariable("userid") Long userid,Model model){
        model.addAttribute("user",userService.findUserById(userid));
        return "pages/user/detail.html";
    }

    @GetMapping("/search")
    public String searchPage(User user, Model model){
        System.out.printf("got in getmapping############################");
        model.addAttribute("users",userService.findUserByTeleAndName(user.getUsername(),user.getTelephone()));
        return "pages/user/search.html";
    }

    @PostMapping("/search")
    public String findUser(User user, Model model){
        System.out.printf("%%%%%%%%%%%%%%%%got in find user");
        model.addAttribute("users",userService.findUserByTeleAndName(user.getUsername(),user.getTelephone()));
        return "/pages/user/list.html";
    }
}
